package top.melody.ecommerce.platform.init;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import top.melody.ecommerce.common.util.page.PageRows;
import top.melody.ecommerce.common.util.page.Pagination;
import top.melody.ecommerce.platform.api.domain.basedata.Region;
import top.melody.ecommerce.platform.api.service.BasedataService;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * CreateUser: renhuan
 * Email:richiedryday@gmail.com
 * CreateTime: 2015/3/17 16:31
 * ModifyUser: renhuan
 * ModifyTime: 2015/3/17 16:31
 * Class Description:
 * To change this template use File | Settings | File Templates.
 */
public class InitialCachePostProcessor implements ApplicationListener<ContextRefreshedEvent> {

    private static final Logger logger = LoggerFactory.getLogger(InitialCachePostProcessor.class);
    @Resource
    private BasedataService basedataService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (event.getApplicationContext().getParent() == null) {
            logger.debug("begin to initial cache......");
            logger.debug("start to write b base data region cache......");
//            try {
//                int count = basedataService.getRegionCount(null);
//                Pagination page = new Pagination(count, 1, 10);
//                PageRows<Region> firstPage = basedataService.queryRegionListByPage(null, page);
//                int totalPage = firstPage.getPage().getMaxPage();
//                for (Region region : firstPage.getRows()) {
//                    EcommerceCache.getInstance().putRegion2Cache(region);
//                }
//                for (int index = 2; index <= totalPage; index++) {
//                    Pagination queryPage = new Pagination(count, index, 10);
//                    List<Region> list = basedataService.queryRegionListByPage(null, queryPage).getRows();
//                    for (Region region : list) {
//                        EcommerceCache.getInstance().putRegion2Cache(region);
//                    }
//                }
//            } catch (Exception e) {
//                logger.error("init region cache error", e);
//            }
        }
    }
}
